import axios from 'axios'
import a from './../main'

const host = process.env.NODE_ENV === 'development' ? '/api' : 'http://118.89.227.78:3000'

/* function createInstance() {
  let instance = host ? axios.create({baseURL: host}) : axios.create()
  // instance.defaults.headers.common['X-XSRF-TOKEN'] = `${sessionStorage.getItem('token')}`
  return instance
} */
axios.defaults.withCredentials = true

export const get = (url, params) => {
  return new Promise((resolve, reject) => {
    axios.get(host + url, {params: params}).then(data => {
      resolve(data.data)
    }).catch(error => {
      if (error.response.status === 401) {
        a.$router.push({name: 'logIn'})
      } else {
        reject(error)
      }
    })
  })
}

export const post = (url, params) => {
  return new Promise((resolve, reject) => {
    axios.post(host + url, params).then(data => {
      resolve(data.data)
    }).catch(error => {
      if (error.response.status === 401) {
        console.log('跳转')
      } else {
        reject(error)
      }
    })
  })
}

export const getImageUrl = pp => {
  if (host === '/api') {
    return 'http://localhost:3000/' + pp
  } else {
    return host + '/' + pp
  }
}
